package com.lixin.springcloud.controller;

import com.lixin.springcloud.entities.CommonResult;
import com.lixin.springcloud.entities.Payment;
import com.lixin.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @PostMapping("/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("插入结果：{}", result);
        if (result <= 0) {
            return new CommonResult<>(10000, "插入失败", null);
        }
        return new CommonResult<>(20000, "插入成功 port：" + port, payment);
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询成功：{}", payment);
        if (payment == null) {
            return new CommonResult<>(10000, "查询失败 port：" + port, null);
        }
        return new CommonResult<>(20000, "查询成功 port：" + port, payment);
    }

    // 用于测试OpenFeignClient调用超时
    @GetMapping("/payment/timeout")
    public String paymentTestTimeout() {
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return port;
    }

}
